from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.Utils import BlogDriver
#博客列表页
class BlogList:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_list.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
    def ListTestByLogin(self):
        """测试博客列表页并点击进入详情页"""
        wait = WebDriverWait(self.driver, 2)

        # 验证列表页元素
        wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.title")))
        # ... 其他验证 ...

        # 注意：这里不获取URL，仅用于测试列表页功能
        # 真正的URL获取在 get_first_blog_url() 中完成
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > a").click()

        # 验证是否进入详情页
        wait.until(EC.visibility_of_element_located((By.XPATH, "//button[contains(text(), '编辑')]")))
        print("成功进入详情页")
    def get_first_blog_url(self):
        """获取第一个博客的URL但不跳转"""
        wait = WebDriverWait(self.driver, 5)
        try:
            # 定位第一个博客的链接元素
            link_element = wait.until(
                EC.visibility_of_element_located(
                    (By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > a"))
            )
            url = link_element.get_attribute('href')
            print(f"获取到的博客URL: {url}")
            return url
        except Exception as e:
            print(f"获取URL失败: {e}")
            BlogDriver.getScreeShot()  # 保存截图用于调试
            return None
    #测试首页登录状态下
    def ListTestByLogin(self):
        #测试博客标题是否存在（找不到博客标题元素的问题）只能添加显示等待
        # 添加等待，确保页面加载完成
        wait = WebDriverWait(self.driver, 2)
        # 等待博客标题元素出现
        wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.title")))
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.title")
        #测试博客内容是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.desc")
        #测是博客用户名是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        #测试博客创建时间是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.date")
       #测试博客查看全文按钮是否存在
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > a")
        #添加测试截图
        BlogDriver.getScreeShot()
        # 测试博客查看全文按钮是否功能正常,点击查看是否正常进入博客详情页
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > a").click()
        #等待详情页加载完毕
        wait.until(EC.visibility_of_element_located((By.XPATH, "/html/body/div[2]/div[2]/div/div[4]/button[1]")))
        #获取编辑按钮文本确定真的进入博客详情页
        button = self.driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div[4]/button[1]")
        print("按钮文本:", button.text)



